const moment = require('moment');

module.exports = class extends think.Model {
    get relation() {
        return {
            order_goods: {
                type: think.Model.HAS_MANY,
                key: 'id',
                fKey: 'order_id'
            },
            order_aftersale: {
                relation: false,
                type: think.Model.HAS_MANY,
                key: 'id',
                fKey: 'order_id'
            }
        };
    }

    async orderadd(orderInfo, details) {
        //开启事务
        return await this.transaction(async() => {
            const orderId = await this.add(orderInfo);
            // 统计商品详情
            for (const goodsItem of details) {
                const size_list = JSON.parse(goodsItem.size_list);
                console.log(size_list);
                const size = await this.model('user_size').where({phone: size_list.phone, user_id: ['>', 0]}).find();
                let sizeid = 0;
                if (think.isEmpty(size)) {
                    const order_size_id = await this.model('user_size').db(this.db()).add({
                        phone: size_list.phone,
                        username: size_list.username,
                        // age: size_list.age,
                        height: size_list.height,
                        weight: size_list.weight,
                        parter_id: think.user.user_id
                    });
                    sizeid = order_size_id;
                } else {
                    await this.model('user_size').db(this.db()).where({id: size.id}).update({
                        username: size_list.username,
                        age: size_list.age,
                        height: size_list.height,
                        weight: size_list.weight,
                        parter_id: think.user.user_id
                    });
                    sizeid = size.id;
                }
                // const order_size_id = await this.model('order_goods_size').db(this.db()).add({
                //     username: size.username, age: size.age, height: size.height, weight: size.weight,
                //     waist_size: size.waist_size, chest: size.chest, shoulder: size.shoulder,
                //     abdomen: size.abdomen, back: size.back, thingh: size.thingh,
                //     other: size.other, img: size.img
                // });

                let data = {
                    order_id: orderId,
                    goods_order_no: '62000' + moment().format('YYYYMMDDHHmmss') + Math.random().toString().substr(2, 8),
                    goods_id: goodsItem.goods_id,
                    goods_name: goodsItem.goods_name,
                    goods_no: goodsItem.goods_no,
                    primary_pic_url: goodsItem.primary_pic_url,
                    sale_price: goodsItem.sale_price,
                    number: goodsItem.number,
                    // custom_style: goodsItem.custom_style,
                    is_words: goodsItem.is_words,
                    words_desc: goodsItem.words_desc,
                    words_id: goodsItem.words_id,
                    words_color_id: goodsItem.words_color_id,
                    status: goodsItem.status,
                    versionid: goodsItem.versionid,
                    sizeid: size_list.sizeid,
                    usersizeid: size_list.user_size_id||sizeid,
                    sizecodeid: size_list.sizecodeid,
                    custom_desc: '',
                    size_desc: size_list.size_desc || '',
                    customsize_id: size_list.customsize_id,
                    content: '',
					peiliao: goodsItem.peiliao_list,
					cateid:goodsItem.cateid,
					list_data:goodsItem.list_data,
					size_list:goodsItem.size_list,
					volume_listNew:goodsItem.volume_listNew
                };
                let insertId = await this.model('order_goods').db(this.db()).add(data);
                const custom_style = JSON.parse(goodsItem.custom_style);
                let custom = [];
                for (const cs of custom_style) {
                    custom.push({
                        order_goods_id: insertId,
                        customid: cs.customid,
                        styleid: cs.styleid,
                        stylechildid: cs.stylechildid,
                        mar_id: cs.mar_id,
                        marter_id: cs.marter_id,
                        content: ''
                    });
                }
                await this.model('order_custom').db(this.db()).addMany(custom);
                // let size_lists = [];
                // for (const cs of size_list.list) {
                //     size_lists.push({
                //         order_goods_id: insertId,
                //         tailoredid: cs.tailoredid,
                //         size: cs.size,
                //         content: '',
                //         sub_size:cs.sub_size
                //     });
                // }
                // await this.model('order_size').db(this.db()).addMany(size_lists);
            }
            return orderId;
        });
    }

    // async test() {
    //     await this.transaction(async() => {
    //         const insertId = await this.model('atest').db(this.db()).add({name: '0987655555'});
    //         const userCate = this.model('atests').db(this.db());
    //         let result = await userCate.add({test: '100', test2: insertId});
    //         return result;
    //     });
    // }
};
